import { CHANGE_HOME_DATA, ADD_LIST, TOGGLE_BACK_TOP_SHOW } from './actionType'
import Axios from 'axios'

export const changeHomeData = (topicList, articleList, recommendList) => ({
  type: CHANGE_HOME_DATA,
  topicList,
  articleList,
  recommendList,
})

export const getHomeData = () => {
  return (dispatch, getState) => {
    Axios.get('/api/home.json')
      .then((res) => {
        if (res?.data?.success) {
          const { topicList, articleList, recommendList } = res.data.data
          dispatch(changeHomeData(topicList, articleList, recommendList))
        }
      })
      .catch((err) => console.error(err))
  }
}
export const addList = (list, nextPage) => ({
  type: ADD_LIST,
  list,
  nextPage,
})

export const getMoreList = (page) => {
  return (dispatch, getState) => {
    Axios.get('/api/homeList.json?page=' + page).then((res) => {
      if (res?.data?.success) {
        const moreList = res.data.data
        dispatch(addList(moreList, page + 1))
      }
    })
  }
}

export const toggleBackTopShow = (flag) => ({
  type: TOGGLE_BACK_TOP_SHOW,
  flag,
})
